package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.Cart;
import com.neusoft.elmboot.po.CartDTO;
import com.neusoft.elmboot.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/CartController")
public class CartController {

    @Autowired
    private CartService cartService;

    @RequestMapping("/listCart")
    public Map<String, Object> listCart(Cart cart) throws Exception {
        List<Cart> cartList =  cartService.listCart(cart);
        List<CartDTO> cartDTOList = new ArrayList<>();

        // 将 Cart 转换为 CartDTO
        for (Cart c : cartList) {
            CartDTO cartDTO = new CartDTO();
            cartDTO.setCartId(c.getCartId());
            cartDTO.setFoodId(c.getFoodId());
            cartDTO.setBusiness(c.getBusiness());
            cartDTO.setQuantity(c.getQuantity());
            cartDTO.setBusinessId(c.getBusinessId());  // 返回商家ID
            cartDTO.setFood(c.getFood());
            cartDTOList.add(cartDTO);
        }

        Map<String, Object> response = new HashMap<>();
        response.put("result", cartDTOList); // 将封装好的 CartDTO 列表返回
        return response;
    }


    @RequestMapping("/saveCart")
    public Map<String, Object> saveCart(@RequestBody Cart cart) throws Exception {
        // 返回的响应结构
        Map<String, Object> response = new HashMap<>();
        
        // 调用服务层保存购物车项
        int result = cartService.saveCart(cart);
        
        // 根据结果设置返回值
        if (result > 0) {
            response.put("result", 1); // 表示保存成功
        } else {
            response.put("result", 0); // 表示保存失败
        }

        return response;
    }


    @RequestMapping("/updateCart")
	public Map<String, Object> updateCart(@RequestBody Cart cart) throws Exception {
        Map<String, Object> response = new HashMap<>();
        
        // 调用服务层更新购物车项
        int result = cartService.updateCart(cart);
        
        // 根据更新结果设置返回值
        if (result > 0) {
            response.put("result", 1); // 更新成功
        } else {
            response.put("result", 0); // 更新失败
        }

        return response;
    }
    
    @RequestMapping("/removeCart")
    public Map<String, Object> removeCart(@RequestBody Cart cart) throws Exception {
        Map<String, Object> response = new HashMap<>();

        // 调用服务层删除购物车项
        int result = cartService.removeCart(cart);

        // 根据删除结果设置返回值
        if (result > 0) {
            response.put("result", 1); // 删除成功
        } else {
            response.put("result", 0); // 删除失败
        }

        return response;
    }
}
